package com.example.myapplication.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.customview.widget.ViewDragHelper
import androidx.databinding.DataBindingUtil
import androidx.drawerlayout.widget.DrawerLayout
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityDrawerBinding
import com.yalantis.ucrop.util.ScreenUtils
import java.lang.reflect.Field


/**
 * @author: rongfeng
 * @date: 2022/3/10
 * @description
 */
class DrawerActivity : AppCompatActivity() {
    private var mPosX = 0f;
    private var mPosY = 0f;
    /** 记录移动后抬起坐标点（终点）**/
    private var mCurPosX = 0f;
    private var mCurPosY = 0f;

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityDrawerBinding = DataBindingUtil.setContentView(
            this, R.layout.activity_drawer)
        binding.idDrawerFrame.addTouchViews(binding.btn1)
//        id_drawer_frame. init(view_pager)

        setDrawerLeftEdgeSize(this,binding.idDrawerLayout,0.3f)

//        val params: ViewGroup.LayoutParams = nav_view.getLayoutParams()
//        params.width = ((ScreenUtils.getScreenWidth(this) * 0.4f).toInt())
//        nav_view.setLayoutParams(params)


//
//        id_drawer_frame.setOnTouchListener { view, event ->
//            when (event.action) {
//                    MotionEvent.ACTION_DOWN->{
//                        mPosX = event.x;
//                        mPosY = event.y;
//                    }
//                  MotionEvent.ACTION_MOVE-> {
//                      mCurPosX = event.x;
//                      mCurPosY = event.y;
//
//                      if (mCurPosY - mPosY > 0
//                          && (Math.abs(mCurPosY - mPosY) > 25)) {
//                          //向下滑動
//                          println("sssss 向下滑動" +event.getAction())
//                          (view as ViewGroup).requestDisallowInterceptTouchEvent(true)
//
//                      } else if (mCurPosY - mPosY < 0
//                          && (Math.abs(mCurPosY - mPosY) > 25)) {
//                          //向上滑动
//                          println("sssss 向上滑动" +event.getAction())
//                      }
//                  }
//
//            }
//            true
//        }
    }



    /**
     * 抽屉滑动范围控制
     *
     * @param activity
     * @param drawerLayout
     * @param displayWidthPercentage 占全屏的份额0~1
     */
    private fun setDrawerLeftEdgeSize(
        activity: Activity?,
        drawerLayout: DrawerLayout?,
        displayWidthPercentage: Float
    ) {
        if (activity == null || drawerLayout == null) {
            return
        }
        try {
            // find ViewDragHelper and set it accessible
            drawerLayout.setScrimColor(Color.TRANSPARENT)
            val leftDraggerField = drawerLayout.javaClass.getDeclaredField("mRightDragger")
            leftDraggerField.isAccessible = true
            val leftDragger = leftDraggerField[drawerLayout] as ViewDragHelper
            // find edgesize and set is accessible
            val edgeSizeField = leftDragger.javaClass.getDeclaredField("mEdgeSize")
            edgeSizeField.isAccessible = true
            val edgeSize = edgeSizeField.getInt(leftDragger)
            // set new edgesize
            edgeSizeField.setInt(
                leftDragger, Math.max(
                    edgeSize,
                    (ScreenUtils.getScreenWidth(this) * displayWidthPercentage).toInt()
                )
            )
//            edgeSizeField.setInt(
//                leftDragger, 20
//            )

            //获取 Layout 的 ViewDragCallBack 实例“mLeftCallback”
            //更改其属性 mPeekRunnable
            val leftCallbackField: Field =
                drawerLayout.javaClass.getDeclaredField("mRightCallback")
            // true 表示不可长按弹窗
            leftCallbackField.isAccessible = true

            //因为无法直接访问私有内部类，所以该私有内部类实现的接口非常重要，通过多态的方式获取实例
            val leftCallback: ViewDragHelper.Callback =
                leftCallbackField[drawerLayout] as ViewDragHelper.Callback
            val peekRunnableField: Field = leftCallback.javaClass.getDeclaredField("mPeekRunnable")

            // true 表示不可长按弹窗
            peekRunnableField.isAccessible = true
            peekRunnableField[leftCallback] = Runnable {}
        } catch (e: NoSuchFieldException) {
        } catch (e: IllegalArgumentException) {
        } catch (e: IllegalAccessException) {
        }
    }

}